package com.wy.jetpack.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.wy.jetpack.R;
import com.wy.jetpack.viewmodel.LiveDataVM;

public class LiveDataActivity extends AppCompatActivity {

    private TextView tv_cxk, tv_jay;
    private Button bt_cxk, bt_jay;
    private LiveDataVM liveDataVM;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_livedata);


        tv_cxk = findViewById(R.id.tv_cxk);
        tv_jay = findViewById(R.id.tv_jay);

        bt_cxk = findViewById(R.id.bt_cxk);
        bt_jay = findViewById(R.id.bt_jay);


        liveDataVM = new ViewModelProvider(this).get(LiveDataVM.class);


        liveDataVM.getCxk().observe(this, new Observer<Integer>() {
            @Override
            public void onChanged(Integer integer) {
                tv_cxk.setText(String.valueOf(liveDataVM.getCxk().getValue()));
            }
        });


        liveDataVM.getJay().observe(this, new Observer<Integer>() {
            @Override
            public void onChanged(Integer integer) {
                tv_jay.setText(String.valueOf(liveDataVM.getJay().getValue()));
            }
        });


        bt_cxk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                liveDataVM.addCxk();
            }
        });


        bt_jay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                liveDataVM.addJay();
            }
        });


    }
}
